//
//  GYBleOperationManager.h
//  LockManagement
//
//  Created by apple on 2018/7/31.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <CoreBluetooth/CoreBluetooth.h>


/** 设备扫描状态的枚举 */
typedef NS_ENUM(NSUInteger, searchFlagType) {
    searchFlagTypeDefaut = 1 << 0,
    searchFlagTypeFinish = 1 << 1,//扫描时间到
    searchFlagTypeDisconnect = 1 << 2,//设备断开连接 删除设别
    searchFlagTypeAdded  = 1 << 3,//扫描到新设备
    searchFlagTypeChanged= 1 << 4,//已经扫描到设备，设备的状态改变
    searchFlagTypeDelete = 1 << 5 ,//设备超过时间未被发现
};

/** 设备连接状态的枚举 */
typedef NS_ENUM(NSUInteger, deviceConnectType){
    deviceConntTypeSuccess = 1, //设备了解成功
    deviceConntTypeFailed, //设备连接失败
    deviceConntTypeFiledTimeOut, //设备连接失败,(连接超时)
    deviceConntTypeDisConnect, //设别断开连接
};

/**
 搜索到设备时,都会调用这个蓝牙设备
 
 @param peripheral 搜索到的设备
 @param searchType 通知外部, 对外设备的操作类型
 */
typedef void(^blueManagerSearchDeviceBlock)(CBPeripheral *peripheral , searchFlagType searchType);

/**
 蓝牙状态发改变的时候回调blcok
 
 @param centerMnaager 中心设备manager
 @param state 状态
 */
typedef void(^blueManagerStateChangedBlock)(CBCentralManager *centerMnaager, CBManagerState state);

/**
 设备连接回调
 
 @param periphreal 连接的设备
 @param error 连接错误
 @param deviceConnectType 连接状态
 */
typedef void(^blueManagerConnectDeviceBlock)(CBPeripheral *periphreal, NSError *error, deviceConnectType deviceConnectType);

/**
 读取RSSI之后的回调
 
 @param periphreal 外围设备
 @param RSSI 外围设备的RSSI值
 @param error 错误描述
 */
typedef void(^blueManagerReadRSSIBlock)(CBPeripheral *periphreal, NSNumber *RSSI, NSError *error);

/**
 搜索到服务时候的回调
 
 @param periphreal 外围设备
 @param serviceArray 该设备下的所有服务
 @param error 错误
 */
typedef void(^blueManagerSearchDeviceServiceBlock)(CBPeripheral *periphreal, NSArray<CBService *> *serviceArray, NSError *error);

/**
 发现服务上的特征时候调用
 
 @param characteristicArray 所有特征
 @param error <#error description#>
 */
typedef void(^blueManagerSearchCharacteristicBlock)(CBPeripheral *periphreal, NSArray<CBCharacteristic *> *characteristicArray, NSError *error);

/**
 蓝牙返回数据

 @param data 返回的数据
 */
typedef void(^blueManagerCharacteristicReturnDataBlock)(CBCharacteristic *characteristic, NSData *data);

/**
 监听蓝牙连接设备的状态

 @param connectState 设备此时的链接状态
 */
typedef void(^blueManagerConnectDeviceStateBlock)(deviceConnectType connectState);

@interface GYBleOperationManager : NSObject

#pragma mark -- centerMnaager

/** 中心管理者 */
@property (nonatomic, strong, readonly) CBCentralManager *centerMnaager;

/** 连接的外围设备 */
@property (nonatomic, strong, readonly) CBPeripheral *periphreal;

/** 当前设备的链接状态 */
@property (nonatomic, assign) deviceConnectType connectType;

/** 是否正在扫面设备 */
@property (nonatomic, getter=isScanning) BOOL scanning;

/** 当前蓝牙状态改变block */
@property (nonatomic, copy) blueManagerStateChangedBlock blueManagerStateChangedCallBack;

#pragma mark -- periphreal

/** 连接设备回调 */
@property (nonatomic, copy) blueManagerConnectDeviceBlock blueManagerConnectDeviceCallBack;

/** 发现服务的时候回调 */
@property (nonatomic, copy) blueManagerSearchDeviceServiceBlock blueManagerSearchDeviceServiceCallBack;

/** 发现服务上的特征回调 */
@property (nonatomic, copy) blueManagerSearchCharacteristicBlock blueManagerSearchCharacteristicCallBack;

/** 收到蓝牙返回数据回调 */
@property (nonatomic, copy) blueManagerCharacteristicReturnDataBlock blueManagerCharacteristicReturnDataCallBack;

/** 设备连接状态改变的时候调用 */
@property (nonatomic, copy) blueManagerConnectDeviceStateBlock blueManagerConnectDeviceStateCllBack;

#pragma mark -- 初始化

+ (instancetype)shareOperationManager;

#pragma mark -- 扫描

/**
 扫描设备
 
 @param searchDeviceCallBack <#searchDeviceCallBack description#>
 */
- (void)scanDeviceWithCallback:(blueManagerSearchDeviceBlock)searchDeviceCallBack  ;

/**
 扫面设备
 
 @param timeInterval 扫描时间
 @param searchDeviceCallBack <#searchDeviceCallBack description#>
 */
- (void)scanDeviceWithTimeInterval:(NSTimeInterval)timeInterval
                          callBack:(blueManagerSearchDeviceBlock)searchDeviceCallBack  ;

/**
 根据条件扫描设备
 
 @param timeInterval 扫描时间
 @param service 服务条件
 @param options 扫描配置
 @param searchDeviceCallBack <#searchDeviceCallBack description#>
 */
- (void)scanDeviceWithTimeInterval:(NSTimeInterval)timeInterval
                          services:(NSArray *)service
                           options:(NSDictionary *)options
                          callBack:(blueManagerSearchDeviceBlock)searchDeviceCallBack ;

/**
 停止扫描设备
 */
- (void)stopScanDevice;

#pragma mark -- 链接外围设备

/**
 连接设备
 
 @param timeOut 超时时间
 @param options 连接设备时间
 @param callBack <#callBack description#>
 */
- (void)connectDevice:(CBPeripheral *)periphreal withTimeOut:(NSTimeInterval)timeOut options:(NSDictionary *)options callBack:(blueManagerConnectDeviceBlock)callBack;

/**
 主动断开设备 (不会回调设备失去连接方法)
 */
- (void)disConnectDevice;

#pragma mark -- 扫描服务

/**
 根据服务id条件 来查找设备中的所有服务
 
 @param uuidArray <#uuidArray description#>
 @param callback <#callback description#>
 */
//- (void)discoverDeviceServiceWithUUIDArray:(NSArray<CBUUID *> *)uuidArray
//                                  callback:(blueManagerSearchDeviceServiceBlock)callback;

/**
 在服务上发现特征

 @param uuidArray <#uuidArray description#>
 @param callback <#callback description#>
 */
- (void)discoverCharacteristicWithCharacteristicUUIDs:(NSArray<CBUUID *> *)uuidArray
                                             callback:(blueManagerSearchCharacteristicBlock)callback ;




#pragma mark -- 发送消息命令

- (void)sendMessage:(NSData *)data;









@end
